const client = require('../pkg/client')
const openapi = require('../pkg/openapi')
require('dotenv').config()

// 使用环境变量获取AK、SK
// 如果没有设置，需要报错退出
if (!process.env.CTYUN_AK || !process.env.CTYUN_SK) {
    console.error('Environment variables CTYUN_AK and CTYUN_SK must be set.');
    process.exit(1);
}

const cfg = new client.Config( //按顺序完成config构造
    "cf.fc-xinan1.ctyun.cn", 
    process.env.CTYUN_AK, 
    process.env.CTYUN_SK, 
    // "72fe863adec94cd3a42a9309dcd30c00",
    // "b26255c97b18465cb58449e5befde4f8",
    "https",
    "POST");

const c = new client.Client(cfg);
const request = new openapi.InvokeWorkflowRequest("grey-0325","exec-test", '{"a":1}');
const headers = new openapi.InvokeWorkflowHeaders({}, process.env.CTYUN_REGION_ID || '200000002368'); //regionID默认使用西南1
const service = new openapi.InvokeWorkflowService(c);

// const response = new openapi.QueryFunctionResponse();
(async() => {
    try {
        console.log('start example workflow invoke.');
        const data = await service.invokeWorkflow(request, headers);
        console.log('example workflow invoke return with: ', data);

    } catch (error) {
      console.log('example workflow invokey return with error: ', error.message);
    }
})();

cfg.endpoint = "fc-global.ctapi.ctyun.cn";
const query_c = new client.Client(cfg);
const query_servier = new openapi.QueryWorkflowService(query_c);
(async() => {
    try {
        console.log('start example workflow query.');
        const data = await query_servier.queryWorkflow(request, headers);
        console.log('example workflow query return with: ', data);
    } catch (error) {
      console.log('example workflow query return with error: ', error.message);
    }
})();
